<?php
// 
class IndexAction extends SCAction {
    public function index(){
	    		
        $this->display();
		//C('Installer_setup_n')
    }
	public function check(){
	// $config=C();
	// $config['Installer_setup_n']='check';
	// C($config);
    // C($config,'BY_USER_SET_CONFIG');	
	 
	$next=U('Index/setup');
	 $check_msg['phpversion']['info']=phpversion();
	 if(phpversion() < '5.2.0'){
	 	$check_msg['phpversion']['error']='php版本过低';
        $next ='';
	}
	$check_msg['mysql']['info']='';
	 if(extension_loaded('mysql')){
	 	$check_msg['mysql']['info']='mysql';
	 }else{
	 	$check_msg['mysql']['error']='mysql不支持';
        $next ='';	 

	 }
	$check_msg['file_uploads']['info']='';
	 if(@ini_get('file_uploads')){
	 	$check_msg['file_uploads']['info']='最大上传字节'.@ini_get('upload_max_filesize');
	 }else{
	 	$check_msg['file_uploads']['error']='file_uploads不支持';
        $next ='';	 
	 }
	$check_msg['gd']['info']='';
	 if(function_exists("gd_info")){
	 	//$check_msg['gd']['info']=gd_info();
		$gdinfo=gd_info();
		$check_msg['gd']['info']=$gdinfo["GD Version"];
	 }else{
	 	$check_msg['gd']['error']='GD库不支持';
        $next ='';	 
	 }
	 
	 		//需要检测目录、文件
		$check_file=array(
			'./weiw',
			'./weiw/Conf/config.php',
			'./weiw/Modules',
			'./weiw/Modules/admin2',
			'./weiw/Modules/admin_c',
			'./weiw/Modules/weiw',
		);		
		$error_msg=array();
		foreach ($check_file as $file){
			//检测文件是否存在
			if (!file_exists($file)){
				$error_msg[]=$file.' 不存在！';
				$next ='';
				continue;
			}
			if (is_dir($file)){
				//检测目录是否可写				
				$file_test=@fopen($file.'/test.txt','w');
				if(!$file_test){
					$error_msg[] = $file." 不可写!";
					$next ='';
				}
				@fclose($file_test);
				@unlink($file.'/test.txt');
			}else {
				//检测文件是否可写
				if (!is_writeable($file)) {
					$error_msg[] = $file." 不可写!";
					$next ='';
				}
			}
		}
	 $this->nexturl=$next;
	 $this->file_msg=$error_msg;
	 $this->check_msg=$check_msg;
	 $this->display(); 
	}
	public function setup(){
	 $this->display();
	}
	public function setcon(){
	//	 $config=C();
	 $config['Installer_setup_n']='finish';
	// $config['']='';
	$errstr="";
	$config['DB_TYPE']='mysql';
	if ($_POST['db_host']!="") $config['DB_HOST']=$_POST['db_host'];
	if ($_POST['db_name']!=""){
	$config['DB_NAME']=$_POST['db_name'];
	}else{
	$errstr="数据库名称不能为空<br>";
	};
	
	if ($_POST['db_user']!=""){
	$config['DB_USER']=$_POST['db_user'];
	}else{
	$errstr.="用户名称不能为空<br>";
	};
	if ($_POST['db_pwd']!=""){
	$config['DB_PWD']=$_POST['db_pwd'];
	}else{
	$errstr.="密码不能为空<br>";
	};
	if ($_POST['db_port']!="")$config['DB_PORT']=$_POST['db_port'];
	if ($_POST['db_prefix']!=""){
	$config['DB_PREFIX']=$_POST['db_prefix'];
	}else{
	$errstr.="表前缀不能为空<br>";
	};
     if ($errstr!="" ){
	    $this->error($errstr);
	 }
	 else
	 {
	 //判断数据库可用性未写代码。
     C($config);
	 $Data = M(); 
       if($Data->execute('
CREATE TABLE IF NOT EXISTS `'.$config['DB_PREFIX'].'index` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` tinytext NOT NULL,
  `alink` tinytext NOT NULL,
  `ordi` int(11) NOT NULL DEFAULT "0",
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ;

             ')===false){
			  $info.='<br>创建'.$config['DB_PREFIX'].'index表失败</br>';
			  $this->error($info);
			}else{
			  $info.='<br>创建'.$config['DB_PREFIX'].'index表完成</br>';
			};
       if($Data->execute('
CREATE TABLE IF NOT EXISTS `'.$config['DB_PREFIX'].'info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pid` int(11) NOT NULL DEFAULT "0",
  `title` tinytext NOT NULL,
  `info` text NOT NULL,
  `datetime` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ;

             ')===false){
			  $info.='<br>创建'.$config['DB_PREFIX'].'info表失败</br>';
			  $this->error($info);
			}else{
			  $info.='<br>创建'.$config['DB_PREFIX'].'info表完成</br>';
			};
	   if($Data->execute('
CREATE TABLE IF NOT EXISTS `'.$config['DB_PREFIX'].'info_index` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` tinytext NOT NULL,
  `type` smallint(6) NOT NULL DEFAULT "1",
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;

             ')===false){
			  $info.='<br>创建'.$config['DB_PREFIX'].'info_index表失败</br>';
			  $this->error($info);
			}else{
			  $info.='<br>创建'.$config['DB_PREFIX'].'info_index表完成</br>';
			};
	   if($Data->execute('
CREATE TABLE IF NOT EXISTS `'.$config['DB_PREFIX'].'menu` (
  `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
  `pid` smallint(4) unsigned NOT NULL,
  `cname` varchar(50) NOT NULL DEFAULT "",
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;

             ')===false){
			  $info.='<br>创建'.$config['DB_PREFIX'].'menu表失败</br>';
			  $this->error($info);
			}else{
			  $info.='<br>创建'.$config['DB_PREFIX'].'menu表完成</br>';
			};
	   if($Data->execute('
CREATE TABLE IF NOT EXISTS `'.$config['DB_PREFIX'].'products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pid` int(11) NOT NULL DEFAULT "0",
  `title` tinytext NOT NULL,
  `img` tinytext NOT NULL,
  `info` text NOT NULL,
  `datetime` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

             ')===false){
			  $info.='<br>创建'.$config['DB_PREFIX'].'products表失败</br>';
			  $this->error($info);
			}else{
			  $info.='<br>创建'.$config['DB_PREFIX'].'products表完成</br>';
			};
	   if($Data->execute('
CREATE TABLE IF NOT EXISTS `'.$config['DB_PREFIX'].'products_index` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` tinytext NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

             ')===false){
			  $info.='<br>创建'.$config['DB_PREFIX'].'products_index表失败</br>';
			  $this->error($info);
			}else{
			  $info.='<br>创建'.$config['DB_PREFIX'].'products_index表完成</br>';
			};											
	   if($Data->execute('
CREATE TABLE IF NOT EXISTS `'.$config['DB_PREFIX'].'users` (
  `username` varchar(60) NOT NULL,
  `password` varchar(64) NOT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

             ')===false){
			  $info.='<br>创建'.$config['DB_PREFIX'].'users表失败</br>';
			  $this->error($info);
			}else{
			  $info.='<br>创建'.$config['DB_PREFIX'].'users表完成</br>';
			};
		$utb=D("Users");
		$userdata['username'] = $_POST['username'];
   		$userdata['password'] = $_POST['password'];
		$userdata['repassword'] = $_POST['repassword'];
		if($utb->create($userdata)) { 
      		if($utb->add()) {
			  //  dump($config);
				
				C($config,'BY_USER_SET_CONFIG');
			//	dump($config);
			//	exit();
        		 $this->success($info.'安装成功！','Finish/finish',10);
      		 }else{
          		 $this->error('账户数据写入错误！');
   		     }
		//return $result;
  	    }else{
  	     	 $this->error($utb->getError());
	    }
	 };
	}
}
?>